package com.wqc.common.util;

import org.springframework.util.Assert;

import java.lang.reflect.Field;
import java.math.BigDecimal;

public class ReflectionUtils {

    public static <T> boolean isBaseType(Class<T> paramType)
    {
        return ((paramType == String.class) || (paramType == Integer.class) || (paramType == Integer.TYPE) || (paramType == Short.class) || (paramType == Short.TYPE) || (paramType == Byte.class) || (paramType == Byte.TYPE) || (paramType == Long.class) || (paramType == Long.TYPE) || (paramType == Float.class) || (paramType == Float.TYPE) || (paramType == Double.class) || (paramType == Double.TYPE) || (paramType == Character.class) || (paramType == Character.TYPE) || (paramType == Boolean.class) || (paramType == Boolean.TYPE) || (paramType == BigDecimal.class));
    }

    public static Field[] getAccessibleFields(Object obj)
    {
        Assert.notNull(obj, "object不能为空");
        Class superClass = obj.getClass();
        for (; superClass != Object.class; superClass = superClass.getSuperclass()) {
            try {
                Field[] fields = superClass.getDeclaredFields();
                for (Field field : fields) {
                    field.setAccessible(true);
                }
                return fields;
            } catch (SecurityException localSecurityException) {
            }
        }
        return null;
    }

    public static Field getAccessibleField(Object obj, String fieldName)
    {
        Assert.notNull(obj, "object不能为空");
        Assert.hasText(fieldName, "fieldName");
        Class superClass = obj.getClass();
        for (; superClass != Object.class; superClass = superClass.getSuperclass()) {
            try {
                Field field = superClass.getDeclaredField(fieldName);
                field.setAccessible(true);
                return field;
            } catch (NoSuchFieldException localNoSuchFieldException) {
            }
        }
        return null;
    }

    public static Object getFieldValue(Object obj, String fieldName)
    {
        Field field = getAccessibleField(obj, fieldName);
        if (field == null) {
            throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]");
        }

        Object result = null;
        try {
            result = field.get(obj);
        }
        catch (IllegalAccessException localIllegalAccessException) {
        }
        return result;
    }

    public static void setFieldValue(Object obj, String fieldName, Object value)
    {
        Field field = getAccessibleField(obj, fieldName);
        if (field == null) {
            throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]");
        }
        try
        {
            field.set(obj, value);
        }
        catch (IllegalAccessException localIllegalAccessException)
        {
        }
    }
}
